package org.lex.ftp.ui.speed;

import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;

import org.lex.awt.ComponentUtilities;
import org.lex.awt.Location;
import org.lex.ftp.FTP;
import org.lex.ftp.ui.MessageDialog;
import org.lex.ftp.ui.find.old.speed.FileEntryNameFilter;
import org.lex.ftp.ui.find.old.speed.SpeedTestSearchFrame;
import org.lex.swing.SelectAllWhenGainFocus;
import org.lex.swing.widgets.JMenuButton;


public class ToolBar extends JToolBar implements ActionListener {
	private static final String LoadAC = "Load";
	private static final String TestAC = "test";
	// action-command
	private static final String ClearNoSuchFileAC = "nofile";
	private static final String ClearFailedAC = "failed";
	private static final String ClearBothAC = "both";
	private static final String ClearAllAC = "all";
	private static final String AdvancedAC = "advanced";

	private final SpeedPane pane;
	private MessageDialog inputDialog = null;
	private JTextField targetTextField = null;
	private TestManager testManager = null;

	public ToolBar(SpeedPane pane) {
		super();
		this.pane = pane;
		this.add(this.createButton("Load", LoadAC));
		this.add(this.createButton("Test", TestAC));
		this.add(this.createClearButton());
		this.addSeparator();
		this.add(this.createTargetLabel());
		this.addSeparator(new Dimension(2, 2));
		this.add(this.getTargetTextField());
		JButton advancedButton = this.createButton("...", AdvancedAC);
		advancedButton.setToolTipText("Advanced Search Condition");
		this.add(advancedButton);
		this.testManager = new TestManager(pane, pane.getTableModel());
	}

	private JLabel createTargetLabel() {
		JLabel targetLabel = new JLabel();
		targetLabel.setText("Target");
		return targetLabel;
	}

	private JTextField getTargetTextField() {
		if (null == targetTextField) {
			targetTextField = new JTextField();
			targetTextField.addFocusListener(new SelectAllWhenGainFocus());
		}
		return targetTextField;
	}

	private JMenuButton createClearButton() {
		JMenuButton clearButton = new JMenuButton();
		clearButton.setMaximumSize(new Dimension(30, 30));
		clearButton.setText("Clear");
		clearButton.setActionCommand(ClearFailedAC);
		clearButton.add(createMenuItem("Connetion failed", ClearFailedAC));
		clearButton.add(createMenuItem("Target not found", ClearNoSuchFileAC));
		clearButton.add(createMenuItem("Both of them", ClearBothAC));
		clearButton.addSeparator();
		clearButton.add(createMenuItem("All", ClearAllAC));
		clearButton.addActionListener(this);
		return clearButton;
	}

	private JButton createButton(String text, String actionCommand) {
		JButton button = new JButton();
		button.setText(text);
		button.setActionCommand(actionCommand);
		button.addActionListener(this);
		return button;
	}

	private JMenuItem createMenuItem(String text, String actionCommand) {
		JMenuItem menu = new JMenuItem(text);
		menu.setActionCommand(actionCommand);
		return menu;
	}

	private MessageDialog getInputDialog() {
		if (null == this.inputDialog) {
			this.inputDialog = new MessageDialog();
			this.inputDialog.setTitle("Input");
			this.inputDialog.setPrompt("ÿ��һ��Ҫ���Եķ�������ַ");
			this.inputDialog.setAlwaysOnTop(true);
			this.inputDialog.getTextArea().getDocument().addDocumentListener(new InputDocumentListener(pane, inputDialog.getTextArea()));
			Location.moveToParentCenter(pane, inputDialog);
		}
		return this.inputDialog;
	}

	public void actionPerformed(ActionEvent e) {
		String ac = e.getActionCommand();
		if (LoadAC == ac) {
			this.getInputDialog().setVisible(true);
		} else if (TestAC == ac) {
			String fileName = this.getTargetTextField().getText();
			if (null == fileName || 0 == fileName.trim().length()) {
				JOptionPane.showMessageDialog(FTP.getApplication().getRootWindow(), "������������Ϊ��", "������", JOptionPane.ERROR_MESSAGE);
				return;
			}
			testManager.setFilter(new FileEntryNameFilter(fileName.trim()));
			testManager.startTest(true);
		} else if (ClearFailedAC == ac) {
			this.pane.getTableModel().clearConnectFailed();
		} else if (ClearNoSuchFileAC == ac) {
			this.pane.getTableModel().clearNoSuchFile();
		} else if (ClearBothAC == ac) {
			this.pane.getTableModel().clearBoth();
		} else if (ClearAllAC == ac) {
			this.pane.getTableModel().clearAll();
		} else if (AdvancedAC == ac) {
			SpeedTestSearchFrame find = new SpeedTestSearchFrame(testManager);
			find.setSize(500, 420);
			Window root = ComponentUtilities.getAncestor(Window.class, pane);
			Location.moveToParentCenter(root, find);
			find.setVisible(true);
		}
	}
}
